package ve.usb.ddp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import ve.usb.cohesion.runtime.CohesionAction;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.cohesion.runtime.HibernateUtil;
import ve.usb.ddp.hibernate.*;


/**
 * Manejo de elementos de la entidad Pas.<br>
 */
public class AccionesPas extends CohesionAction {
    
/**
     * Called by Struts for the execution of action APreBuscar_pas.
     * Prepara la vista de selección de criterios de búsqueda para una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreBuscar_pas(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VBuscar_pas", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPasBuscar.clear(mapping, request);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreCrear_pas.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrear_pas(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrear_pas", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPas fFPas = FPas.clear(mapping, request);

             //Feed for combo listProfesor.
             {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
                (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
                "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
                "from Profesor").list();
              list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
              fFPas.setListProfesor(list);
             }


            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ABuscar_pas.
     * Captura los criterios de búsqueda y los fija en la sesión.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ABuscar_pas(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreListar_pas", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPasBuscar fFPasBuscar = (FPasBuscar)form;
            request.getSession().setAttribute("ABuscar_pas_campo", "fecha_inicio");
            request.getSession().setAttribute("ABuscar_pas_valor", "%" + fFPasBuscar.getFecha_inicio() + "%");

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreListar_pas.
     * Selecciona las entidades a ser listadas de acuerdo a los parámetros recibidos.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreListar_pas(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VListar_pas", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            String crit = (String)request.getSession().getAttribute("ABuscar_pas_campo");
            java.util.List<Pas> list;
            if (crit==null) {
                list = (java.util.List<Pas>)s.createQuery("from Pas").list();
            } else {
                String valor = (String)request.getSession().getAttribute("ABuscar_pas_valor");
                list = (java.util.List<Pas>)
                   s.createQuery("from Pas where "+crit+" like :valor")
                    .setString("valor", valor).list();
                request.getSession().removeAttribute("ABuscar_pas_campo");
                request.getSession().removeAttribute("ABuscar_pas_valor");
            }
            request.setAttribute("L_Pas", list);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APre_pas.
     * Prepara la vista de actualización de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APre_pas(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_pas", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idPas = lookup_Long(request, "idPas");
            
            FPas fFPas = FPas.clear(mapping, request);
            Pas pas = (Pas)s.load(Pas.class, idPas);
            fFPas.setIdPas(pas.getIdPas());

            fFPas.setProfesor(pas.getProfesor().getIdProfesor());

            fFPas.setFecha_inicio(pas.getFecha_inicio().toString());
            fFPas.setFecha_finalizacion(pas.getFecha_finalizacion().toString());
            fFPas.setProf_desc_cadivi(pas.getProf_desc_cadivi());
            fFPas.setUniversidad(pas.getUniversidad());
            fFPas.setPais(pas.getPais());

            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPas.setListProfesor(list);
            }

            for (Observacion_Pas _Observacion_Pas : pas.getObservaciones()) {
                Hibernate.initialize(_Observacion_Pas.getPas());
            }
            request.setAttribute("L_Observacion_Pas", pas.getObservaciones());
            for (Monto _Monto : pas.getMontos()) {
                Hibernate.initialize(_Monto.getPas());
            }
            request.setAttribute("L_Monto", pas.getMontos());


            request.getSession().setAttribute("idProfesor_super", fFPas.getProfesor());

            request.getSession().setAttribute("idPas", idPas);
            request.getSession().setAttribute("idPas_super", idPas);

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AAct_pas.
     * Registro de actualizaciones de una entidad.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AAct_pas(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_pas", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPas fFPas = (FPas)form;
            Pas pas = (Pas)s.load(Pas.class, fFPas.getIdPas());
            pas.setFecha_inicio(new ve.usb.cohesion.runtime.LocalDate(fFPas.getFecha_inicio()));
            pas.setFecha_finalizacion(new ve.usb.cohesion.runtime.LocalDate(fFPas.getFecha_finalizacion()));
            pas.setProf_desc_cadivi(fFPas.getProf_desc_cadivi());
            pas.setUniversidad(fFPas.getUniversidad());
            pas.setPais(fFPas.getPais());


            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPas.setListProfesor(list);
            }

            for (Observacion_Pas _Observacion_Pas : pas.getObservaciones()) {
                Hibernate.initialize(_Observacion_Pas.getPas());
            }
            request.setAttribute("L_Observacion_Pas", pas.getObservaciones());
            for (Monto _Monto : pas.getMontos()) {
                Hibernate.initialize(_Monto.getPas());
            }
            request.setAttribute("L_Monto", pas.getMontos());

            request.getSession().setAttribute("idPas", pas.getIdPas());
            request.getSession().setAttribute("idPas_super", pas.getIdPas());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AAct_pas.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action AElim_pas.
     * Elimina la entidad seleccionada.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward AElim_pas(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"APreListar_pas", "V_pas", };
        final int SALIDA_0 = 0;
        final int SALIDA_1 = 1;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            Long idPas = lookup_Long(request, "idPas");
            Pas pas = (Pas)s.load(Pas.class, idPas);
            s.delete(pas);
            try {
                s.flush();
            } catch (Exception ex1) {
                ex1.printStackTrace();
                salida = SALIDA_1;
            }

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_pas.msg0"));
        }
        if (salida==1) {
          request.setAttribute("msg",
            getResources(request).getMessage("AElim_pas.msg1"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action ACrear_pas.
     * Registro de la creación de una Pas.<br>
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward ACrear_pas(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"V_pas", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPas fFPas = (FPas)form;
            Pas pas = new Pas();
            pas.setFecha_inicio(new ve.usb.cohesion.runtime.LocalDate(fFPas.getFecha_inicio()));
            pas.setFecha_finalizacion(new ve.usb.cohesion.runtime.LocalDate(fFPas.getFecha_finalizacion()));
            pas.setProf_desc_cadivi(fFPas.getProf_desc_cadivi());
            pas.setUniversidad(fFPas.getUniversidad());
            pas.setPais(fFPas.getPais());

            s.save(pas);
            s.flush();
            fFPas.setIdPas(pas.getIdPas());

            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPas.setListProfesor(list);
            }

            request.getSession().setAttribute("idPas", pas.getIdPas());
            request.getSession().setAttribute("idPas_super", pas.getIdPas());

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        if (salida==0) {
          request.setAttribute("msg",
            getResources(request).getMessage("ACrear_pas.msg0"));
        }

        return mapping.findForward(SALIDAS[salida]);
    }

/**
     * Called by Struts for the execution of action APreCrear_pas_profesor.
     * Prepara la vista de captura de datos de una entidad para realizar una inserción.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @return The Struts name of the following step.
     * @throws java.lang.Exception For untreated exceptions. 
     * These exceptios will normally be treated with 
     * the default exception action.
     */
    public ActionForward APreCrear_pas_profesor(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        //Salidas
        final String[] SALIDAS = {"VCrear_pas_profesor", };
        final int SALIDA_0 = 0;

        int salida = SALIDA_0;
//Checking for actors administrador
            if (!CohesionActor.checkActor(request, 1)) {
                return mapping.findForward(CohesionActor.SALIDA_ACTOR);
            }
        Session s = HibernateUtil.getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
            FPas fFPas = FPas.clear(mapping, request);

            //Feed for combo listProfesor.
            {java.util.List<ve.usb.cohesion.runtime.Pair> list = 
               (java.util.List<ve.usb.cohesion.runtime.Pair>)s.createQuery(
               "select new ve.usb.cohesion.runtime.Pair(ci, idProfesor) " + 
               "from Profesor").list();
             list.add(0,new ve.usb.cohesion.runtime.Pair("Ninguna", 0));
             fFPas.setListProfesor(list);
            }

            fFPas.setProfesor(Long.parseLong((String)request.getParameter("idProfesor_super")));

            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        } finally {
            try { s.close(); } catch (Exception ex2) {}
        }
        return mapping.findForward(SALIDAS[salida]);
    }



}